from rest_framework import serializers

from users.models import User


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = "__all__"
        extra_kwargs = {
            "password": {
                "write_only": True
            }
        }

    def create(self, validated_data):
        instance = super().create(validated_data)
        password = instance.password
        instance.set_password(password)
        instance.is_staff = True
        instance.save()
        return instance

    def update(self, instance, validated_data):
        instance = super().update(instance,validated_data)
        password = instance.password
        instance.set_password(password)
        instance.save()
        return instance
